let TodoList = (
    function () {
        // 数据和数据操作-没有事件侦听和DOM操作
        // 私有变量
        let tasks = JSON.parse(localStorage.getItem('tasks')) || []
        function saveToLocalStorage() {
            localStorage.setItem('tasks', JSON.stringify(tasks))
        }
        function renderTasks(filteredTasks) {
            const ul = document.querySelector(".tasklist ul");
            ul.innerHTML = '';
            filteredTasks.forEach(task => {
                const li = document.createElement('li');
                li.innerHTML = `<div class="left">
                            <input type="checkbox" data-id="${task.id}" ${task.completed ? 'checked' : ''}>
                            <span class="${task.completed ? 'line' : ''}">${task.text}</span>
                          </div>
                          <div class="btn-group">
                            <button class="editBtn" data-id="${task.id}">编辑</button>
                            <button class="delBtn" data-id="${task.id}">删除</button>
                          </div>`;
                ul.appendChild(li);
            });
            updateCount();
        }
        // 添加
        function addTask(text) {
            const newTask = { id: tasks.length > 0 ? tasks[tasks.length - 1].id + 1 : 1, text: text, completed: false };
            tasks.push(newTask);
            saveToLocalStorage();
            renderTasks(tasks);
        }
        // 编辑
        function editTask(id, newText) {
            const task = tasks.find(t => t.id == id);
            if (task) {
                task.text = newText;
                saveToLocalStorage();
                renderTasks(tasks);
            }
        }
        // 删除
        function removeTask(id) {
            tasks = tasks.filter(task => task.id != id);
            saveToLocalStorage();
            renderTasks(tasks);
        }
        // 改变状态
        function changeStatus(id, completed) {
            const task = tasks.find(t => t.id == id);
            if (task) {
                task.completed = completed;
                saveToLocalStorage();
                renderTasks(tasks);
            }
        }
        // 分类
        function filterTasks(filter) {
            switch (filter) {
                case "completed": return tasks.filter(task => task.completed);
                case "uncompleted": return tasks.filter(task => !task.completed);
                default: return tasks;
            }
        }
        // 统计
        function updateCount() {
            let all = tasks.length;
            let completed = tasks.filter(task => task.completed).length;
            let uncompleted = all - completed;
            document.querySelector('.footer span:nth-child(1)').textContent = `全部 ${all}`;
            document.querySelector('.footer span:nth-child(2)').textContent = `已完成 ${completed}`;
            document.querySelector('.footer span:nth-child(3)').textContent = `未完成 ${uncompleted}`;
        }
        function getTasks() {
            return tasks;
        }

        return {
            saveToLoalStorage: saveToLocalStorage,
            addTask: addTask,
            renderTasks: renderTasks,
            removeTask: removeTask,
            changeStatus: changeStatus,
            filterTasks: filterTasks,
            updateCount: updateCount,
            editTask:editTask,
            getTasks:getTasks
        }
        // 
    }
)()